package com.medeiros.upmoney.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medeiros.upmoney.data.LimiteDataProvider;
import com.medeiros.upmoney.model.Limite;
import com.medeiros.upmoney.util.ViewUtil;

public class TecladoGastos extends Activity {

	private Limite original;
	private LimiteView limite;
	private float buffer = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = this.getIntent();
		original = (Limite) intent.getSerializableExtra("limite");
		Limite lim = original.clone();
		limite = new LimiteView(lim, this, null, android.R.attr.progressBarStyleHorizontal);

		LayoutInflater inflater = getLayoutInflater(); 
		LinearLayout ll =	(LinearLayout) inflater.inflate(R.layout.teclado, null);
		LinearLayout cabecalho = (LinearLayout) ll.findViewById(R.id.cabecalho);
		cabecalho.addView(limite,1);
		TextView tv = new TextView(this);
		tv.setText(lim.getCategoria().getNome());
		cabecalho.addView(tv);

		setContentView(ll);
		registrarBotoes();
	}

	private void reset(){
		this.buffer = 0;
		Limite lim = original.clone();
		this.limite.setLimite(lim);
		this.update();
	}

	protected void cancelar() {
		this.finish();
	}

	protected void confirmar() {
		LimiteDataProvider.getInstance().write(this,  limite.getLimite());
		this.finish();
	}

	private void adicionar(float valor) {
		this.buffer += valor;
		this.limite.addGasto(valor);
		this.update();		
	}

	private void update(){
		TextView textView = getTextView();
		textView.setText("R$ "+ String.valueOf(buffer));
		this.limite.update();
	}

	private TextView getTextView() {
		TextView bufferGasto = (TextView) this.findViewById(R.id.bufferGasto);
		return bufferGasto;
	}

	private void registrarBotoes() {

		registrarBotao(new OnClickListener() {
			public void onClick(View v) {
				TecladoGastos.this.confirmar();
			}
		}, R.id.confirmar);

		registrarBotao(new OnClickListener() {
			public void onClick(View v) {
				TecladoGastos.this.reset();
			}
		}, R.id.limpar);

		registrarBotao(new OnClickListener() {
			public void onClick(View v) {
				TecladoGastos.this.cancelar();
			}
		}, R.id.cancelar);


		OnClickListener listenerAdicao = new OnClickListener() {
			public void onClick(View v) {
				BotaoGasto btn = (BotaoGasto) v;
				float valor = btn.getValor();
				TecladoGastos.this.adicionar(valor);
			}
		};

		registrarBotao(listenerAdicao,
				R.id.botaoGasto1, 
				R.id.botaoGasto2, 
				R.id.botaoGasto3, 
				R.id.botaoGasto4, 
				R.id.botaoGasto5, 
				R.id.botaoGasto6, 
				R.id.botaoGasto7, 
				R.id.botaoGasto8, 
				R.id.botaoGasto9);
	}

	private void registrarBotao(OnClickListener listenerAdicao,	int... ids) {
		ViewUtil.registrarClickListener(this,listenerAdicao, ids);
	}

}
